package cn.tedu.weibo.controller;

import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.response.JsonResult;
import cn.tedu.weibo.service.WeiboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/v1/weibos/")
public class WeiboController {

    @Autowired
    private WeiboService weiboService;

    @PostMapping("add-new")
    public JsonResult addNew(WeiboDTO weiboDTO){
        weiboService.insert(weiboDTO);
        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult listForIndex(){
        return JsonResult.ok(weiboService.listForIndex());
    }

    /*
    * @GetMapping 注解是基于@RequestMapping注解的,如果不写值,则等价于
    * http://localhost:8080/v1/weibos
    * eg:同钟请求方式智能有一个这样写,不然找不到
    * */
    @GetMapping("selectByid")
    public JsonResult listForIndexByUserId(Long userId){
        return JsonResult.ok(weiboService.listForIndexByUserId(userId));
    }

    /*
    *
    *    @PostMapping("{id}/delete")
    *       路径里的变量属性值要和前端写的一样
    *   axios.post('http://localhost:8080/v1/weibos/' + id + '/delete')
    *
    *   @PathVariable("id") 可以获取到请求中的信息
    * */
    @PostMapping("{id}/delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        weiboService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("{id}/detail")
    public JsonResult DetailById(@PathVariable Long id){
        log.debug("id = " + id);
        WeiboDetailVO weiboDetailVO = weiboService.detailById(id);
        return JsonResult.ok(weiboDetailVO);
    }


}
